package com.MonkWorks.timedormitory;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author Jacob
 * 
 */
public class ShowLocationActivity extends MapActivity {

	private GeoPoint point;
	private Button btnCoord;
	private Button button_back;
	private TextView address;
	private TextView longitude;
	private TextView latitude;
	private String Address;
	private String job;
	private int longi;
	private int lat;
	private String clientName;
	private RadioButton satellite;
	private RadioButton map;
	
	private void initComponents(){
		address = (TextView) findViewById(R.id.textAddress);
		longitude = (TextView) findViewById(R.id.textLong);
		latitude = (TextView) findViewById(R.id.textLat);
		map = (RadioButton) findViewById(R.id.rb_map);
		satellite = (RadioButton) findViewById(R.id.rb_satellite);
		MapView mapView = (MapView) findViewById(R.id.map);
		mapView.setBuiltInZoomControls(true);
		// Create map overlay list
		List<Overlay> mapOverlays = mapView.getOverlays();
		// adds the monk emblem to the map for a marker
		Drawable drawable = this.getResources().getDrawable(R.drawable.monk88);
		HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
				drawable, this);
		// gets extras from the client page
		getExtras();
		Geocoder geocoder = new Geocoder(this, Locale.getDefault());
		try {
			List<Address> geoResults = geocoder.getFromLocationName(Address, 4);
			// gives variables longi and lat values of the address location
			longi = (int) (geoResults.get(0).getLongitude() * 1E6);
			lat = (int) (geoResults.get(0).getLatitude() * 1E6);
			// assigns the coordinates to point
			point = new GeoPoint(lat, longi);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// Creates the overlay item on the map
		OverlayItem overlayitem = new OverlayItem(point, "", job);
		itemizedoverlay.addOverlay(overlayitem);
		mapOverlays.add(itemizedoverlay);

		// displays the address and corresponding longitude and latitude on the
		// activity page
		address.setText(Address);
		longitude.setText(Integer.toString(longi));
		latitude.setText(Integer.toString(lat));

		btnCoord = (Button) findViewById(R.id.btnCoordinates);
		// button that adds the coordinates to the client page
		btnCoord.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// sends extras longi and lat to the client page
				Intent i = new Intent(v.getContext(), ClientActivity.class);
				i.putExtra("latitude", lat);
				i.putExtra("longitude", longi);
				i.putExtra("job", job);
				i.putExtra("address", Address);
				i.putExtra("clientName", clientName);
				startActivity(i);
				popup("Coordinates Added");

			}
		});

		button_back = (Button) findViewById(R.id.btnBack);
		// button that adds the coordinates to the client page
		button_back.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// sends extras longi and lat to the client page
				Intent i = new Intent(v.getContext(), ClientActivity.class);
				i.putExtra("job", job);
				i.putExtra("clientName", clientName);
				startActivity(i);
				popup("No coordinates were added");

			}
		});

		RadioGroup rgViews = (RadioGroup) findViewById(R.id.rg_views);
		//check listener for radio group
		//Allows user to switch between satellite and map views
		OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener() {  
			@Override            
			public void onCheckedChanged(RadioGroup group, int checkedId) {               

				MapView mapView = (MapView) findViewById(R.id.map);                
				// Currently checked is rb_map                
				if(checkedId==R.id.rb_map){                 
					mapView.setSatellite(false);           
				}            
				// Currently checked is rb_satellite    
				if(checkedId==R.id.rb_satellite){         
					mapView.setSatellite(true);             
				}      
			}      
		};       

		rgViews.setOnCheckedChangeListener(checkedChangeListener);   
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_location);
		initComponents();
		
	}


	private void getExtras() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			job = extras.getString("job");
			Address = extras.getString("address");
			clientName = extras.getString("clientName");
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_show_location, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		MapView mapView = (MapView) findViewById(R.id.map);  
		//Logs the user out
		if (item.getItemId() == R.id.Logoff) {

			Intent  MIntent = new Intent(getBaseContext(),LoginActivity.class);
			startActivityForResult(MIntent, 0);
			popup("You have logged out");
			return true;
		}
		else if (item.getItemId() == R.id.Satellite) {

			mapView.setSatellite(true);
			satellite.setChecked(true);
			return true;
		}
		else if (item.getItemId() == R.id.map) {
			mapView.setSatellite(false);
			map.setChecked(true);
			return true;
		}


		return (super.onOptionsItemSelected(item));
	}

	protected void popup(String message) {
		// this is how we make a popup.
		Toast.makeText(this, message, Toast.LENGTH_LONG).show();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}